// const sql = require('mssql');

var {Sequelize,DataTypes} = require("sequelize");
let { dboption } = require("./conf");
const sequelize = new Sequelize(dboption);
const { Op } = require("sequelize");

var project = require('./models/base_conpermit_project')(sequelize, DataTypes);
var consproject = require('./models/base_consproject')(sequelize, DataTypes);
var zaj = require('./models/base_project_subject')(sequelize, DataTypes);

//sequelize-auto -o "./models" -h 60.211.159.133 -p 8090 -d zj_cycle  -u sa -x Hxy971123 -e mssql -t base_conpermit_project
//sequelize-auto -o "./models" -h 60.211.159.133 -p 48086 -d zj_cycle  -u root -x shuyan123 -e mysql -t base_conpermit_project
//npm install -g mssql tedious
(async () => {
    try {
        // make sure that any items are correctly URL encoded in the connection string
        // await sql.connect('Server=60.211.159.133,8090;Database=zj_cycle;User Id=sa;Password=Hxy971123;trustServerCertificate=true;')
        // const result = await sql.query `select top 1 * from base_conpermit_project`;
        // result.recordset.forEach(row => {
        // })
        // const res = await model.findAll({where:{id:{ [Op.lte]: 2}}});
        const res = await consproject.findAll();
        res.forEach(async row => {
            let data = row.toJSON();
            // console.log(data);
            let codes = data['质安监系统中查询subjectID']?.replace(/；/g,';').split(';');
            codes?.forEach(async code=>{
                // await sequelize.query(`insert into baserel_consproject_conpermit values(${data.id},'${code}')`);
                await zaj.update({ id_consproject:data.id  }, {
                    where: {
                        subjectID: code.trim()
                    }
                  });
            })
        });

    } catch (err) {
        console.log('异常', err.toString())
    }
})()